/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package schedulingScheme;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author Andrzej
 */
public class Helper {
    public static <T> List<List<T>> getAllSubsets(List<T> set){
        List<List<T>> subsets = new ArrayList<List<T>>();

        subsets.add(Collections.EMPTY_LIST);
        
        for (int i= 1; i<= set.size(); i++){
            subsets.addAll(getAllNElementSubsets(set, i));

        }
        return subsets;
    }
    public static <T> List<List<T>> getAllNElementSubsets(List<T> set, int numberOfElements){
        int[] currentIndexesInSet = new int[numberOfElements];
        List<List<T>> subsets = new ArrayList<List<T>>();
        for (int i=0; i< numberOfElements; i++){
            currentIndexesInSet[i] = i;
        }
        subsets.add(subset(currentIndexesInSet, set));
        while(increment(currentIndexesInSet, set.size())){
            subsets.add(subset(currentIndexesInSet, set));
        }

        return subsets;
    }

    private static <T> List<T> subset(int[] indexes, List<T> set){
        List<T> res = new ArrayList<T>(indexes.length);
        for (int i: indexes){
            res.add(set.get(i));
        }
        return res;
    }

    private static boolean increment(int[] currentIndexesInSet, int size) {
        int maxIndex = size - 1;
        for (int i=currentIndexesInSet.length - 1, j=0;i >= 0; i--, j++){
            if (currentIndexesInSet[i] < maxIndex - j){
                currentIndexesInSet[i] += 1;
                for (int k= i + 1;k < currentIndexesInSet.length; k++){
                    currentIndexesInSet[k] = currentIndexesInSet[k-1] + 1;
                }
                return true;
            }
        }
        return false;
    }

}
